export class CountDownUtils {
  /**
   * 倒计时插件 返回 日 时 分 秒
   * ot  String 入参 '2020-01-01 00:00:00' 或 '2020/01/01 00:00:00'
   * fn   function 回调函数。参数是返回的数据 格式：{'DD':'00','HH':'00','mm':'00','ss':'ss'}
   * bool  Boolean 是否开启倒计时。 可不传 ， 默认为 true
   *
   *  调用示例：
   * CountDown('2020-01-01 00:00:00', dateTime => {
   *    console.log(dateTime)  // dataTime 即为返回的倒计时结果
   * })
   */
  static CountDown(ot, fn, bool = true) {
    let p = function (t) {
      t = t < 10 ? "0" + t : "" + t;
      return t;
    };
    // var h = window.location.href;
    let i = setInterval(() => {
      // let nh = window.location.href;
      let nt = new Date();
      let lt = ot ? new Date(ot.replace(new RegExp("-", "gm"), "/")) : nt;
      let ct = lt.getTime() - nt.getTime();
      ct = ct > 0 ? ct / 1000 : 0;
      if (bool == false || ct == 0) {
        clearInterval(i);
        fn({
          code: "1",
          warning: "结束倒计时",
          DD: p(0),
          HH: p(0),
          mm: p(0),
          ss: p(0),
          interId: i
        });
      } else {
        let DD = parseInt((ct / 86400).toString());
        let HH = parseInt(((ct / 3600) % 24).toString());
        let mm = parseInt(((ct / 60) % 60).toString());
        let ss = parseInt((ct % 60).toString());
        fn({
          code: "0",
          success: "开始倒计时",
          DD: p(DD),
          HH: p(HH),
          mm: p(mm),
          ss: p(ss),
          interId: i
        });
      }
    }, 1000);
  }
}